package com.iob.coin.DAO;

import com.iob.coin.po.Node;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: MengYuxin
 * @Date: 2021/3/11 15:13
 */


@Repository
public interface NodeDAO extends JpaRepository<Node,String> {

    List<Node>findById(int Id);

    void deleteById(int Id);

    void deleteByKgID(int kgId);

    List<Node> findByKgID(int kgID);

    @Query(nativeQuery = true,value = "select id from se3.node where node_at_id=?1 and kgID=?2")
    int IdByNodeAtIdAndKgId(String NodeAtId,int KgId);

    @Query(nativeQuery = true,value = "select * from se3.node where kgID=?1 and node_at_id=?2")
    List<Node>findNodeByKgIdAndNodeAtId(int kgId,String NodeAtId);

    @Query(nativeQuery = true,value = "select node_at_id from se3.node where kgID=?1 and id=?2")
    String NodeAtIdByKgIdAndId(int kgId,int Id);

    @Query(nativeQuery = true,value = "select type_id from se3.node where kgID=?1 and id=?2")
    int TypeByKgIdAndId(int kgId,int Id);

    @Query(nativeQuery = true,value = "select label_ZH from se3.node where kgID=?1 and id=?2")
    String LabelByKgIdAndId(int kgId,int Id);

    @Query(nativeQuery = true,value = "select count(*) from se3.node where kgID=?1 and type_id=?2")
    int numOfType(int kgID,int type_id);

    @Query(nativeQuery = true,value = "select * from se3.node where kgID=?1 and type_id=?2")
    List<Node>findNodeByKgIDAndType_id(int KgID,int Type_id);

    @Modifying
    @Query(nativeQuery = true,value = "insert into se3.node " +
            "(id, kgID, node_at_id, type_id, label_ZH, x, y, color, radius)" +
            " values (?1,?2,?3,?4,?5,?6,?7,?8,?9)")
    void insert_when_cancel(int id,int kgID,String node_at_id,int type_id,String label_ZH,
                            double x,double y,String color,double radius);

    @Modifying
    @Query(nativeQuery = true,value = "update se3.node set x=-1,y=-1 where kgID=?1")
    void cancelLayout(int kgID);

    @Modifying
    @Query(nativeQuery = true,value = "update se3.node set x=?1,y=?2 where kgID=?3 and  id=?4")
    void saveLayout(double x,double y,int kgID,int id);




}
